#!/usr/bin/perl

# $Id: 03_tega_dbwrapper.t 48 2010-11-13 10:10:51Z dolbanavt@googlemail.com $
# $Date: 2010-11-13 12:11:07 +0200 (Суб, 13 Ноя 2010) $
# $HeadURL: https://tegusigalpa.googlecode.com/svn/branches/storage_init/t/03_tega_dbwrapper.t $
# $LastChangedRevision$

use FindBin qw( $RealBin );
use File::Spec::Functions qw( catfile );

BEGIN {
    push @INC, catfile( $RealBin, '..', 'base_lib' );
    push @INC, catfile( $RealBin, '..', 'lib' );
}

use JIP::ToolSet;
use English qw( -no_match_vars );
use Test::More tests => 26;
use Const::Fast qw( const );
use Test::Exception;
use SQLWrapper;
use version 0.77;

const my $MODULE         => 'Tega::DBWrapper';
const my $WANTED_VERSION => '0.0.1';

use_ok $MODULE;
require_ok $MODULE;

my $has_actual_version = version->parse( $MODULE->VERSION )
    >= version->parse( $WANTED_VERSION );

ok $has_actual_version, 'has actual VERSION';

can_ok $MODULE, qw(
    new _init
    set_connection get_connection
    add_seen_action
);

throws_ok { $MODULE->new(); }
    qr{^Required\soption\s'connection'\sis\snot\sprovided}xms,
    'new( connection )';

my $o = $MODULE->new( connection => 'preved' );

ok $o, 'got object';

isa_ok $o, $MODULE;
isa_ok $o, 'JIP::BaseClass';
isa_ok $o, 'Class::Accessor::Fast';

is $o->get_connection, 'preved', 'connection accessor';

#-- add_seen_action ------------------------------------------------------------
{
    my $w = init_wrapper();

    throws_ok {
        $w->add_seen_action();
    }
    qr{^Required\soption\s'name'\sis\snot\sprovided}xms,
    'add_seen_action( name )';

    throws_ok {
        $w->add_seen_action( name => 'username' );
    }
    qr{^Required\soption\s'action'\sis\snot\sprovided}xms,
    'add_seen_action( action )';

    for my $each_action ( qw( join rename already part quit kick ban ) ) {
        lives_ok {
            $w->add_seen_action(
                name   => 'username',
                action => $each_action,
            );
        } q{add_seen_action( action => '}. $each_action .q{' )};
    }

    throws_ok {
        $w->add_seen_action( name => 'username', action => 'tratata_action' );
    }
    qr{^Key\s'action'\s\(tratata_action\)\sis\sof\sinvalid\stype\sfor}xms,
    '"unexpected_action" exception in add_seen_action()';
}

{
    my $w = init_wrapper();

    my $result = $w->add_seen_action(
        name   => 'username',
        action => 'join',
    );

    is $result, 1, 'add_seen_action() returned true';

    # history testing
    my $dbh_history = $w->get_connection
                        ->get_db_handle
                        ->{'mock_all_history'};

    is scalar @{ $dbh_history },
       1,
       'add_seen_action() - correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       2,
       'add_seen_action() - correct number of parameters bound';

    is $dbh_history->[0]->bound_params->[0],
       'username',
       'correct value for parameter add_seen_action( name )';

    is $dbh_history->[0]->bound_params->[1],
       'join',
       'correct value for parameter add_seen_action( action )';
}

throws_ok {
    my $w = init_wrapper();

    $w->get_connection->get_db_handle->{'mock_can_connect'} = 0;

    $w->add_seen_action(
        name   => 'username',
        action => 'join',
    );
}
qr{cannot\sinsert\sdata:\sNo\sconnection\spresent}xms,
'"cannot_insert" exception in add_seen_action()';

#-- subroutines ----------------------------------------------------------------
sub init_wrapper {
    my $wrapper = SQLWrapper->new(
        dsn     => 'DBI:Mock:',
        user    => q{},
        pass    => q{},
        options => {
            PrintError           => 0,
            RaiseError           => 0,
            AutoCommit           => 1,
        },
    );

    return $MODULE->new( connection => $wrapper );
}

#-- end ------------------------------------------------------------------------
